package pl.lodz.p.ftims.ProjektZespolowy2.managedbeans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
/**
 * 
 */
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import pl.lodz.p.ftims.ProjektZespolowy2.endpoint.EndpointLocal;
import pl.lodz.p.ftims.ProjektZespolowy2.entities.User;

/**
 * @author Łukasz Gadomski
 * 
 */
@ManagedBean
@SessionScoped
public class UserSessionBean {

	@EJB
	private EndpointLocal endpoint;

	private Integer pesel;
	private String password;
	private User user;

	public UserSessionBean() {

	}

	public String logUserIn() {
		System.out.println("logowanie");
		user = endpoint.getUserByPesel(pesel);
		if (user == null) {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage("Nieznany login!"));
			return null;
		} else {
			if (endpoint.checkPasswordForUser(pesel, password)) {
				return "index";
			} else {
				FacesContext.getCurrentInstance().addMessage(null,
						new FacesMessage("Hasło nieprawidłowe!"));
				return null;
			}
		}
	}
	
	public String logUserOut() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index";
    }

	public Integer getPesel() {
		return pesel;
	}

	public void setPesel(Integer pesel) {
		this.pesel = pesel;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
